package com.itheima.service.impl;
import	java.util.HashMap;
import java.util.Map;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 代码使我快乐 Administrator on 2019/12/1.
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public void add(CheckGroup checkGroup,Integer[] checkitemIds) {
        checkGroupDao.add(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        this.setCheckgroupAndCheckitem(checkGroupId,checkitemIds);

    }
    public void setCheckgroupAndCheckitem(Integer checkGroupId,Integer[] checkitemIds){
        if(checkitemIds != null && checkitemIds.length > 0){
            for (Integer checkitemId : checkitemIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("checkgroupId",checkGroupId);
                map.put("checkitemId",checkitemId);
                checkGroupDao.setCheckgroupAndCheckitem(map);
            }
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage,pageSize);

        String queryString = queryPageBean.getQueryString();
        if (queryString != null){
            queryString = "%"+queryString+"%";
            queryPageBean.setQueryString(queryString);
        }
        List<CheckGroup> checkGroups = checkGroupDao.findPage(queryPageBean);
        PageInfo<CheckGroup> pageInfo = new PageInfo<>(checkGroups);

        long total = pageInfo.getTotal();
        return new PageResult(total,checkGroups );
    }

    @Override
    public void edit(CheckGroup checkGroup ,Integer[] checkitemIds) {
//        修改检查组的基础信息
        checkGroupDao.edit(checkGroup);
//        清除检查组跟检查项表的联系
        checkGroupDao.deleteCheckItemIds(checkGroup.getId());
//         重新建立检查组跟检查项表的联系
        Integer checkGroupId = checkGroup.getId();
        this.setCheckgroupAndCheckitem(checkGroupId,checkitemIds);
    }

    @Override
    public void deleteCheck(Integer id) {

        checkGroupDao.deleteCheckItemIds(id);

        checkGroupDao.deleteCheck(id);


    }


    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    @Override
    public List<Integer> findByCheckItemIdsAndByCheckGroupId(Integer id) {
        return checkGroupDao.findByCheckItemIdsAndByCheckGroupId(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }


}
